import { createStore } from 'vuex'
import { req_login, req_logout, req_userGet } from '@/api'
import '@/utils/response-utils'
import { response2ElMessage } from '@/utils/response-utils'

const notLoggedInUser = {
  user_id: -1,
  user_name: "",
  email: "",
}

export default createStore({
  state: {
    /** 登录状态 */
    hasLogin: false,

    /** 登录用户 */
    user: notLoggedInUser,

    /** 测试文献,后续删除 */
    documents: [
      {
        id: 1,
        title: 'Sample Document 1',
        authors: ['Author A', 'Author B'],
        abstract: 'This is the abstract of Sample Document 1.',
        keywords: ['Keyword 1', 'Keyword 2'],
        publicationInfo: 'Journal X, Volume 1, Issue 1, Pages 1-10, 2022',
        doi: 'doi:123456789/sample-doi-1',
        collectionDate: '2023-11-20',
      },
      {
        id: 2,
        title: 'Sample Document 2',
        authors: ['Author C', 'Author D'],
        abstract: 'This is the abstract of Sample Document 2.',
        keywords: ['Keyword 3', 'Keyword 4'],
        publicationInfo: 'Journal Y, Volume 2, Issue 2, Pages 20-30, 2023',
        doi: 'doi:123456789/sample-doi-2',
        collectionDate: '2023-11-21',
      },
      {
        id: 3,
        title: 'Sample Document 3',
        authors: ['Author A', 'Author B'],
        abstract: 'This is the abstract of Sample Document 1.',
        keywords: ['Keyword 1', 'Keyword 2'],
        publicationInfo: 'Journal X, Volume 1, Issue 1, Pages 1-10, 2022',
        doi: 'doi:123456789/sample-doi-1',
        collectionDate: '2023-11-20',
      },
      {
        id: 4,
        title: 'Sample Document 4',
        authors: ['Author A', 'Author B'],
        abstract: 'This is the abstract of Sample Document 1.',
        keywords: ['Keyword 1', 'Keyword 2'],
        publicationInfo: 'Journal X, Volume 1, Issue 1, Pages 1-10, 2022',
        doi: 'doi:123456789/sample-doi-1',
        collectionDate: '2023-11-20',
      },
      {
        id: 5,
        title: 'Sample Document 5',
        authors: ['Author A', 'Author B'],
        abstract: 'This is the abstract of Sample Document 1.',
        keywords: ['Keyword 1', 'Keyword 2'],
        publicationInfo: 'Journal X, Volume 1, Issue 1, Pages 1-10, 2022',
        doi: 'doi:123456789/sample-doi-1',
        collectionDate: '2023-11-20',
      },
      {
        id: 6,
        title: 'Sample Document 6',
        authors: ['Author A', 'Author B'],
        abstract: 'This is the abstract of Sample Document 1.',
        keywords: ['Keyword 1', 'Keyword 2'],
        publicationInfo: 'Journal X, Volume 1, Issue 1, Pages 1-10, 2022',
        doi: 'doi:123456789/sample-doi-1',
        collectionDate: '2023-11-20',
      },
    ],
  },
  getters: {
  },
  mutations: {
    clearUser(state) {
      state.hasLogin = false
      state.user = notLoggedInUser
    },
    setUser(state, user) {
      state.hasLogin = true
      state.user = user
    }
  },
  actions: {
    async userGet(context) {
      let response = await req_userGet()
      let data = response.data
      let user = response.data['user']
      switch (data['errno']) {
        case 1:
          context.state.hasLogin = true
          context.state.user = {
            user_id: user['user_id'],
            user_name: user['user_name'],
            email: user['email'],
          }
          break;
        case -3:
          context.state.hasLogin = false
          context.state.user = notLoggedInUser
          break;
        default:
          response2ElMessage(response)
      }
    }
  },
  modules: {
  }
})
